<?php

/**
 * This is the model class for table "nhombaiviet".
 *
 * The followings are the available columns in table 'nhombaiviet':
 * @property integer $id
 * @property integer $menu_id
 * @property string $ten_nhom
 * @property string $mieu_ta
 * @property string $ngay_tao
 * @property string $ngay_sua
 * @property integer $is_active
 * @property string $link
 * @property integer $stt
 *
 * The followings are the available model relations:
 * @property Baiviet[] $baiviets
 * @property Menu $menu
 */
class Nhombaiviet extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'nhombaiviet';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(' ten_nhom', 'required'),
			array('menu_id, is_active, stt', 'numerical', 'integerOnly'=>true),
			array('ten_nhom', 'length', 'max'=>3000),
			array('mieu_ta', 'length', 'max'=>5000),
			array('link', 'length', 'max'=>1000),
			array('ngay_tao, ngay_sua', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, ten_nhom, mieu_ta, ngay_tao, ngay_sua, is_active, Link, stt', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'baiviets' => array(self::HAS_MANY, 'Baiviet', 'dmnhombv_id'),
//            'menu' => array(self::BELONGS_TO, 'Menu', 'menu_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
//            'menu_id'=>'Menu',
			'ten_nhom' => 'Tên Nhóm',
			'mieu_ta' => 'Miêu Tả',
			'ngay_tao' => 'Ngày Tạo',
			'ngay_sua' => 'Ngày Sửa',
			'is_active' => 'Trạng Thái',
			'link' => 'Đường Dẫn',
            'stt' => 'Số Thứ Tự',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
//        $criteria->compare('menu_id',$this->menu_id);
		$criteria->compare('ten_nhom',$this->ten_nhom,true);
		$criteria->compare('mieu_ta',$this->mieu_ta,true);
		$criteria->compare('ngay_tao',$this->ngay_tao,true);
		$criteria->compare('ngay_sua',$this->ngay_sua,true);
		$criteria->compare('is_active',$this->is_active);
		$criteria->compare('link',$this->link,true);
        $criteria->compare('stt',$this->stt,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Nhombaiviet the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    protected function beforeSave()
    {
        // convert to storage format
        $this->ngay_tao = date('Y-m-d', strtotime( $this->ngay_tao));
        $this->ngay_sua = date('Y-m-d', strtotime( $this->ngay_sua));
        return parent::beforeSave();
    }
}
